package com.pristine.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pristine.dao.ITreatmentPackageDao;
import com.pristine.domain.TreatmentPackageEntity;
import com.pristine.service.wsdl.ITreatmentPackageService;
import com.pristine.service.wsdl.TreatmentPackageVO;
import com.pristine.util.EntityToVOConverter;
import com.pristine.util.VOToEntityConverter;

@Service("treatmentPackageService")
@Transactional
public class TreatmentPackageServiceImpl implements ITreatmentPackageService{

	@Autowired
	private ITreatmentPackageDao treatmentPackageDao;
	
	@Override
	public Integer createTreatmentPackage(TreatmentPackageVO treatmentPackageVO){
		TreatmentPackageEntity treatmentPackage = VOToEntityConverter.convert(treatmentPackageVO);
		treatmentPackageDao.add(treatmentPackage);
		return treatmentPackage.getTreatmentPackageId();
	}
	
	@Override
	public Integer updateTreatmentPackage(TreatmentPackageVO treatmentPackageVO){
		TreatmentPackageEntity treatmentPackage = VOToEntityConverter.convert(treatmentPackageVO);
		treatmentPackageDao.update(treatmentPackage);
		
		return treatmentPackage.getTreatmentPackageId();
	}
	
	@Override
	public boolean deleteTreatmentPackage(Integer id){
		try{
			TreatmentPackageEntity entity = treatmentPackageDao.find(id);
			treatmentPackageDao.remove(entity);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	@Override
	public TreatmentPackageVO getTreatmentPackageById(Integer id){
		return EntityToVOConverter.convert(treatmentPackageDao.find(id));
	}
	
	@Override
	public List<TreatmentPackageVO> getAllTreatmentPackage() {
		
		List<TreatmentPackageEntity> list = treatmentPackageDao.list();
		List<TreatmentPackageVO> voList= new ArrayList<TreatmentPackageVO>();
		if(CollectionUtils.isNotEmpty(list)){
			for(TreatmentPackageEntity entity:list){
				voList.add(EntityToVOConverter.convert(entity));
			}
		}
		return voList;
	}
	
}
